#include "set_adc.h"

volatile int adc_buffer0[ADCA_BUFFER_SIZE0];
volatile unsigned char adc_wr_index0=0;
volatile unsigned char adc_rd_index0=0;
volatile unsigned char adc_counter0=0;
volatile char adc_buffer_overflow0=0;

void myInit_ADC(void)
{
	PORTA.DIR = 0x00;	//Set Port A Sebagai Input
	ADCA.CTRLA = ADC_ENABLE_bm;	//Enable ADC
	
	ADCA.CTRLB = ADC_RESOLUTION_12BIT_gc| (1 << ADC_CONMODE_bp) ;	//Resolusi 12 bit right justified , Signed mode
	//ADCA.CTRLB = ADC_RESOLUTION_12BIT_gc|  (0 << ADC_CONMODE_bp) ;	//Resolusi 12 bit right justified , Unsigned mode
	
	ADCA.REFCTRL = ADC_REFSEL_VCC_gc;	//Referensi Internal VCC/1.6
	
	//ADCA.PRESCALER = ADC_PRESCALER_DIV32_gc; //CLKPer = 32MHZ , CLK ADC max = 2 MHz --> Safety ambil 1 MHz
	ADCA.PRESCALER = ADC_PRESCALER_DIV16_gc; //CLKPer = 32MHZ , CLK ADC max = 2 MHz --> Max Performance
	
	/*ADCA.CH0.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc ; //Set Gain 1x dan Single Endeed Mode
	ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN1_gc ; //ADCA Channel 0 diset ke PINA1 (Sensor Suhu NTC)
	
	ADCA.CH1.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc ; //Set Gain 1x dan Single Endeed Mode
	ADCA.CH1.MUXCTRL = ADC_CH_MUXPOS_PIN4_gc ; //ADCA Channel 0 diset ke PINA4*/
	
	/*ADCA.CH0.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_DIFF_gc; //Set Gain 1x dan Differential Mode
	ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN4_gc | ADC_CH_MUXNEG_PIN3_gc; //ADCA Channel 0 Input POS diset ke PINA4 (Menukur GND) Input NEG diset ke GND PAD*/
	
	ADCA.CH0.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc ; //Set Gain 1x dan Single Endeed Unsigned Mode
	ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN4_gc ; //ADCA Channel 0 Input POS diset ke PINA4 (Menukur GND) Input NEG diset ke GND PAD
	
	ADCA.CH1.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc ; //Set Gain 1x dan Single Endeed Unsigned Mode
	ADCA.CH1.MUXCTRL = ADC_CH_MUXPOS_PIN5_gc ; //ADCA Channel 0 Input POS diset ke PINA4 (Menukur GND) Input NEG diset ke GND PAD
	
	ADCA.CH2.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc ; //Set Gain 1x dan Single Endeed Unsigned Mode
	ADCA.CH2.MUXCTRL = ADC_CH_MUXPOS_PIN6_gc ; //ADCA Channel 0 Input POS diset ke PINA4 (Menukur GND) Input NEG diset ke GND PAD
	
	ADCA.CH3.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc ; //Set Gain 1x dan Single Endeed Unsigned Mode
	ADCA.CH3.MUXCTRL = ADC_CH_MUXPOS_PIN7_gc ; //ADCA Channel 0 Input POS diset ke PINA4 (Menukur GND) Input NEG diset ke GND PAD
}


void myEventInit_ADC(void)
{
	PORTA.DIR = 0x00;	//Set Port A Sebagai Input
	ADCA.CTRLA = ADC_ENABLE_bm;	//Enable ADC
	
	//ADCA.CTRLB = ADC_RESOLUTION_12BIT_gc| (1<<4) ;	//Resolusi 12 bit right justified , Signed mode
	ADCA.CTRLB = ADC_RESOLUTION_12BIT_gc  ;	//Resolusi 12 bit right justified , Unsigned mode
	
	ADCA.REFCTRL = ADC_REFSEL_VCC_gc;	//Referensi Internal VCC/1.6
	
	//ADCA.PRESCALER = ADC_PRESCALER_DIV32_gc; //CLKPer = 32MHZ , CLK ADC max = 2 MHz --> Safety ambil 1 MHz
	ADCA.PRESCALER = ADC_PRESCALER_DIV16_gc; //CLKPer = 32MHZ , CLK ADC max = 2 MHz --> Max Performance
	
	ADCA.CH0.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc ; //Set Gain 1x dan Single Endeed Unsigned Mode
	ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN4_gc ; //ADCA Channel 0 Input POS diset ke PINA4 (Menukur GND) Input NEG diset ke GND PAD
	
	ADCA.CH1.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc ; //Set Gain 1x dan Single Endeed Unsigned Mode
	ADCA.CH1.MUXCTRL = ADC_CH_MUXPOS_PIN5_gc ; //ADCA Channel 0 Input POS diset ke PINA4 (Menukur GND) Input NEG diset ke GND PAD
	
	ADCA.CH2.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc ; //Set Gain 1x dan Single Endeed Unsigned Mode
	ADCA.CH2.MUXCTRL = ADC_CH_MUXPOS_PIN6_gc ; //ADCA Channel 0 Input POS diset ke PINA4 (Menukur GND) Input NEG diset ke GND PAD
	
	ADCA.CH3.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc ; //Set Gain 1x dan Single Endeed Unsigned Mode
	ADCA.CH3.MUXCTRL = ADC_CH_MUXPOS_PIN7_gc ; //ADCA Channel 0 Input POS diset ke PINA4 (Menukur GND) Input NEG diset ke GND PAD
	
	//ADCA.EVCTRL = ADC_EVSEL_7_gc | ADC_EVACT_CH0123_gc ;	//Select Event Channel 7 to trigger conversion on CH0, CH1, CH2, and CH3
	//ADCA.EVCTRL = ADC_SWEEP_0123_gc | ADC_EVSEL_7_gc | ADC_EVACT_SWEEP_gc ;	//Select Event Channel 7 to trigger SWEEP Conversion
	ADCA.EVCTRL = ADC_SWEEP_0123_gc | ADC_EVSEL_7_gc | ADC_EVACT_SYNCHSWEEP_gc ;	//Select Event Channel 7 to trigger SWEEP Conversion
	
	ADCA.CH0.INTCTRL = ADC_CH_INTLVL0_bm; //Enable Interrupt untuk CH0
}

int myADC_Bufgetdata(void)
{
	static char data=0;
	
	//while (rx_counter0==0);
	data=adc_buffer0[adc_rd_index0++];
	
	if (adc_rd_index0 == ADCA_BUFFER_SIZE0) adc_rd_index0=0;
	cli();
	--adc_counter0;	//Atomic process
	sei();
	return data;
}

int myADC_getdataCH0(void)
{
	int16_t temp;
	
	ADCA.CTRLA |= ADC_CH0START_bm ; //Start Konversi untuk CH0
	while(!ADCA.CH0.INTFLAGS); //Menunggu Proses konversi selesai
	ADCA.CH0.INTFLAGS = 1; //Clear Flag
	temp = ADCA.CH0RES; //Membaca Hasil
	return temp;
}

int myADC_getdataCH1(void)
{
	int16_t temp;
	
	ADCA.CTRLA |= ADC_CH1START_bm ; //Start Konversi untuk CH0
	while(!ADCA.CH1.INTFLAGS); //Menunggu Proses konversi selesai
	ADCA.CH1.INTFLAGS = 1; //Clear Flag
	temp = ADCA.CH1RES; //Membaca Hasil
	return temp;
}

int myADC_getdataCH2(void)
{
	int16_t temp;
	
	ADCA.CTRLA |= ADC_CH2START_bm ; //Start Konversi untuk CH0
	while(!ADCA.CH2.INTFLAGS); //Menunggu Proses konversi selesai
	ADCA.CH2.INTFLAGS = 1; //Clear Flag
	temp = ADCA.CH2RES; //Membaca Hasil
	return temp;
}

int myADC_getdataCH3(void)
{
	int16_t temp;
	
	ADCA.CTRLA |= ADC_CH3START_bm ; //Start Konversi untuk CH0
	while(!ADCA.CH3.INTFLAGS); //Menunggu Proses konversi selesai
	ADCA.CH3.INTFLAGS = 1; //Clear Flag
	temp = ADCA.CH3RES; //Membaca Hasil
	return temp;
}